﻿using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class XSGB11 : PDCBase
    {
        #region ==BoxNum===========
        public static readonly DependencyProperty BoxNumProperty = DependencyProperty.Register("BoxNum",
            typeof(string), typeof(XSGB11),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnBoxNumChanged));
        public string BoxNum
        {
            get
            {
                return (string)GetValue(BoxNumProperty);
            }
            set
            {
                SetValue(BoxNumProperty, value);
            }
        }
        private static void OnBoxNumChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as XSGB11;
            if (src != null)
            {
                src.BoxNum = (string)e.NewValue; 
            }
        }
        #endregion
        #region=====  vwGB11=======
        CollectionViewSource vwGB11
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwGB11");
            }
        }
        DataRow dwGB11
        {
            get
            {
                if (vwGB11.View == null)
                    return null;
                if ((DataRowView)vwGB11.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwGB11.View.CurrentItem).Row;
            }
        }
        #endregion
        public XSGB11()
        {
            InitializeComponent();
            vm = this.DataContext as MainViewModel;
        }
        /// <summary>
        /// SetProd 当前订单
        /// SetData保存所有订单，但是用于展示今日订单
        /// SetEnv保存所有订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsLoaded)
                return;
            IsLoaded = true;
            if (vm == null)
            {
                vm = this.DataContext as MainViewModel;
            }
        }


        private void btnConvertBySpinLot_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwGB11 == null)
                    return;
                string GLLBEHI = "";
                string vFBPART = txtFBPART.Text.ToString().Trim();
                string vGLBEM = Helper.ConvertGB2312To8859P1(txtGLBEM.Text.ToString().Trim());
                if(vFBPART=="")
                {
                    MessageBox.Show("请先输入纱支纱型");
                    return;
                }
                if(vGLBEM=="")
                {
                    MessageBox.Show("请先输入打包方式");
                    return;
                }
                string FBPART = vFBPART+"          ";
                string GLBEM = vGLBEM + "                                        ";
                string _date = "", _time = "", _stime = "";
                Helper.GetSTime(ref _date, ref _time, ref _stime);
                string DD = INTEXDBC.GetDBDATE();
                string TT = INTEXDBC.GetDBTIME();
                if (DD == "0") DD = _date;
                if (TT == "0") TT = _time;
                string GPSPPART = dwGB11["GPSPPART"].ToString();
                var query = from DataRow row in SetData.ICC_GB1109.AsEnumerable()
                            where row["GPSPPART"].ToString() == GPSPPART
                            select row;
                foreach (DataRow dwGB in query)
                {
                    GLLBEHI = dwGB["GLLBEHI"].ToString();
                    if (dwGB["ABAID"].ToString().Trim().Length == 0)
                    {
                        vm.MAXORD = vm.MAXORD + 1;
                        dwGB["ABAID"] = vm.TERMNO.Trim() + "." + vm.MAXORD;
                    }
                    double GLBSTKG = Convert.ToDouble(dwGB["GLBSTKG"].ToString()) / 1000;
                    double LTARGETQTY = Convert.ToDouble(dwGB["TARGETQTY"].ToString()) / 1000;
                    if (Math.Abs(GLBSTKG - vm.TARGETQTY) < 1.5)
                    {
                        dwGB["TARGETQTY"] = vm.TARGETQTY * 1000;
                    }
                    else
                    {
                        dwGB["TARGETQTY"] = dwGB["GLBSTKG"];
                    }
                    string vkpnum = null;
                    if (dwGB["JP04"].ToString().TrimEnd().Length > 10)
                    {
                        vkpnum = dwGB["JP04"].ToString().TrimEnd();
                    }
                    vm.RemoveAllocatGLLG(dwGB, vkpnum, LTARGETQTY);
                    vm.AllocatGLLG(dwGB, vkpnum);
                    dwGB["JP01"] = "N";
                    dwGB["PROCESSED"] = "Y";
                    dwGB["CHGDATE"] = DD;
                    dwGB["CHGTIME"] = TT;
                    dwGB["CHGUSER"] = vm.app.User;
                    string BEM = GLBEM.Substring(0,40);
                    dwGB["JP10"] = BEM;
                    dwGB["GPFBPART"] = FBPART.Substring(0, 10);

                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnConvert_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string GLLBEHI = "";
                string vFBPART = txtFBPART.Text.ToString().Trim();
                string vGLBEM = Helper.ConvertGB2312To8859P1(txtGLBEM.Text.ToString().Trim());
                if (vFBPART == "")
                {
                    MessageBox.Show("请先输入纱支纱型");
                    return;
                }
                if (vGLBEM == "")
                {
                    MessageBox.Show("请先输入打包方式");
                    return;
                }
                string FBPART = vFBPART + "          ";
                string GLBEM = vGLBEM + "                                        ";
                string _date = "", _time = "", _stime = "";
                Helper.GetSTime(ref _date, ref _time, ref _stime);
                string DD = INTEXDBC.GetDBDATE();
                string TT = INTEXDBC.GetDBTIME();
                if (DD == "0") DD = _date;
                if (TT == "0") TT = _time;
                var query = from DataRow row in SetData.ICC_GB1109.AsEnumerable()
                            //where row["GPSPPART"].ToString() == GPSPPART
                            select row;
                foreach (DataRow dwGB in query)
                {
                    GLLBEHI = dwGB["GLLBEHI"].ToString();
                    double GLBSTKG = Convert.ToDouble(dwGB["GLBSTKG"].ToString()) / 1000;
                    double LTARGETQTY = Convert.ToDouble(dwGB["TARGETQTY"].ToString()) / 1000;
                    if (Math.Abs(GLBSTKG - vm.TARGETQTY) < 1.5)
                    {
                        dwGB["TARGETQTY"] = vm.TARGETQTY * 1000;
                    }
                    else
                    {
                        dwGB["TARGETQTY"] = dwGB["GLBSTKG"];
                    }
                    if (dwGB["ABAID"].ToString().Trim().Length == 0)
                    {
                        vm.MAXORD = vm.MAXORD + 1;
                        dwGB["ABAID"] = vm.TERMNO.Trim() + "." + vm.MAXORD;
                    }
                    string vkpnum = null;
                    if (dwGB["JP04"].ToString().TrimEnd().Length > 10)
                    {
                        vkpnum = dwGB["JP04"].ToString().TrimEnd();
                    }
                    vm.RemoveAllocatGLLG(dwGB, vkpnum, LTARGETQTY);
                    vm.AllocatGLLG(dwGB, vkpnum);
                    dwGB["JP01"] = "N";
                    dwGB["PROCESSED"] = "Y";
                    dwGB["CHGDATE"] = DD;
                    dwGB["CHGTIME"] = TT;
                    dwGB["CHGUSER"] = vm.app.User;
                    string BEM = GLBEM.Substring(0,40);
                    dwGB["JP10"] = BEM;
                    dwGB["GPFBPART"] = FBPART.Substring(0, 10); 
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnAllocateToVKP_Click(object sender, RoutedEventArgs e)
        {
            if (vm.VKPNUM == ""
                || vm.VKPNUM == null
                )
            {
                MessageBox.Show("请选中销售单");
                return;
            }
            string vkp = dwGB11["JP04"].ToString().TrimEnd();
            if (vkp.Length > 10)
            {
                MessageBox.Show("请先取消留位");
                return;
            }
            string _date = "", _time = "", _stime = "";
            Helper.GetSTime(ref _date, ref _time, ref _stime);
            string DD = INTEXDBC.GetDBDATE();
            string TT = INTEXDBC.GetDBTIME();
            if (DD == "0") DD = _date;
            if (TT == "0") TT = _time;
            if (dwGB11["ABAID"].ToString().Trim().Length == 0)
            {
                vm.MAXORD = vm.MAXORD + 1;
                dwGB11["ABAID"] = vm.TERMNO.Trim() + "." + vm.MAXORD;
            }
            double GLBSTKG = Convert.ToDouble(dwGB11["GLBSTKG"].ToString()) / 1000;
            if (Math.Abs(GLBSTKG - vm.TARGETQTY) < 1)
            {
                dwGB11["TARGETQTY"] = vm.TARGETQTY * 1000;
            }
            else
            {
                dwGB11["TARGETQTY"] = dwGB11["GLBSTKG"];
            }
            dwGB11["JP01"] = "N";
            dwGB11["PROCESSED"] = "Y";
            dwGB11["CHGDATE"] = DD;
            dwGB11["CHGTIME"] = TT;
            dwGB11["CHGUSER"] = vm.app.User; 
            vm.AllocatGLLG(dwGB11, vm.VKPNUM);
        }

        private void btnRemoveByVKP_Click(object sender, RoutedEventArgs e)
        {
            double LTARGETQTY = Convert.ToDouble(dwGB11["TARGETQTY"].ToString()) / 1000;
            vm.VKPNUM = dwGB11["JP04"].ToString().TrimEnd();
            vm.RemoveAllocatGLLG(dwGB11, dwGB11["JP04"].ToString().TrimEnd(), LTARGETQTY);
        }
    }
}
